//
//  WMEPhotoTableViewCell.m
//  everyDay_duiTang
//
//  Created by 恩叔 on 16/1/22.
//  Copyright © 2016年 enshu. All rights reserved.
//

#import "WMEPhotoTableViewCell.h"
#import <UIImageView+WebCache.h>
#import <Masonry.h>

#pragma mark - 有图片的cell
@interface WMEPhotoTableViewCell ()<UIScrollViewDelegate>
@property (nonatomic, retain)UILabel *content;/**<主要内容*/
@property (nonatomic, retain)UIImageView *path;/**<图片*/
@property (nonatomic, retain)UIImageView *xiaoxi;/**<左上角的图标*/
@property (nonatomic, retain)UILabel *comment_count;/**<评论的人数*/
@property (nonatomic, retain)UILabel *userName;/**<发布者的名字*/
@property (nonatomic, retain)UILabel *clubName;/**<俱乐部名字*/
@property (nonatomic, retain)UILabel *time;/**<距今何年*/
@property (nonatomic, retain)UILabel *line;/**<分割线*/
@property (nonatomic, retain)UIScrollView *scrollView;/**<滚动视图*/
@property (nonatomic, assign)CGRect originalFrame;/**<图片的原件大小*/
@property (nonatomic, retain)UIImageView *lastImageView;/**<最后的图片大小*/
@end

@implementation WMEPhotoTableViewCell

- (void)dealloc
{
    [_model release];
    [_content release];
    [_path release];
    [_xiaoxi release];
    [_comment_count release];
    [_userName release];
    [_clubName release];
    [_time release];
    [_line release];
    [_scrollView release];
    [_lastImageView release];
    [super dealloc];
}
#pragma mark -初始化
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //内容
        self.content = [[UILabel alloc] init];
        //        _content.backgroundColor = [UIColor yellowColor];
        _content.font = [UIFont systemFontOfSize:13];
        _content.numberOfLines = 2;
        [self.contentView addSubview:_content];
        [_content release];
        
        //图片
        self.path = [[UIImageView alloc] init];
        //        _path.backgroundColor = [UIColor orangeColor];
        [self.contentView addSubview:_path];
        _path.userInteractionEnabled = YES;
        UITapGestureRecognizer *pathTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showZoomImageView:)];
        [_path addGestureRecognizer:pathTap];
        [_path release];
        [pathTap release];
        
        //消息
        self.xiaoxi = [[UIImageView alloc] init];
        //        _xiaoxi.backgroundColor = [UIColor grayColor];
        [self.contentView addSubview:_xiaoxi];
        [_xiaoxi release];
        
        //评论个数
        self.comment_count = [[UILabel alloc] init];
        //        _comment_count.backgroundColor = [UIColor redColor];
        _comment_count.font = [UIFont systemFontOfSize:7];
        _comment_count.textColor = [UIColor grayColor];
        _comment_count.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:_comment_count];
        [_comment_count release];
        
        //用户名
        self.userName = [[UILabel alloc] init];
        //        _userName.backgroundColor = [UIColor lightGrayColor];
        _userName.font = [UIFont systemFontOfSize:10];
        _userName.textColor = [UIColor lightGrayColor];
        [self.contentView addSubview:_userName];
        [_userName release];
        
        //俱乐部名
        self.clubName = [[UILabel alloc] init];
        //        _clubName.backgroundColor = [UIColor yellowColor];
        _clubName.font = [UIFont systemFontOfSize:10];
        _clubName.textColor = [UIColor lightGrayColor];
        [self.contentView addSubview:_clubName];
        [_clubName release];
        
        //距离现在多久
        self.time = [[UILabel alloc] init];
//        _time.backgroundColor = [UIColor yellowColor];
//        _time.backgroundColor = [UIColor blueColor];
        _time.font = [UIFont systemFontOfSize:9];
        _time.textAlignment = NSTextAlignmentRight;
        [self.contentView addSubview:_time];
        [_time release];
        
        //美观用的线
        self.line = [[UILabel alloc] init];
        _line.backgroundColor = [UIColor lightGrayColor];
        [self.contentView addSubview:_line];
        [_line release];
    }
    return self;
}
#pragma mark -重写setting
- (void)setModel:(WMECludModel *)model
{
    if (_model != model) {
        [_model release];
        _model = [model retain];
        _content.text = _model.content;
        
        _comment_count.text = _model.comment_count.stringValue;
        if (_comment_count.text.integerValue > 1000) {
            _comment_count.text = [NSString stringWithFormat:@"%ldk", _comment_count.text.integerValue / 1000];
        }

        _userName.text = _model.userName;
        
        _time.text = [[self class] compareCurrentTime:_model.active_time];
        
        _clubName.text = _model.cludName;
        [_path sd_setImageWithURL:[NSURL URLWithString:_model.path]];

    }
}
#pragma mark - 布局
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    _xiaoxi.frame = CGRectMake(10, 15, 25, 25);
//    [_xiaoxi mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.
//    }];
    _xiaoxi.image = [UIImage imageNamed:@"xiaoxi"];
    
    _comment_count.frame = CGRectMake(10, 18, 25, 15);
    
    CGFloat textHeight = [[self class] heightWithText:_content.text];
    if (textHeight > 15.51 * 2) {
        textHeight = 15.51 * 2 + 1;
    }
    _content.frame = CGRectMake(40, 20, WIDTH - 40 - 10, textHeight);
    
    _path.frame = CGRectMake(40, _content.frame.origin.y + textHeight + 15, 80, 80);
    _path.contentMode = UIViewContentModeScaleAspectFill;
    _path.clipsToBounds = YES;
    
    CGFloat textWidth = [[self class] widthWithText:_userName.text MySize:CGSizeMake(WIDTH - 100, 10) MyFontSize:10];
    
    _userName.frame = CGRectMake(40, _path.frame.origin.y + _path.frame.size.height + 15, textWidth, 10);
    
    _clubName.frame = CGRectMake(40 + textWidth + 10, _path.frame.origin.y + _path.frame.size.height + 15, 100, 10);
    
    CGFloat timeWidth = [[self class] widthWithText:_time.text MySize:CGSizeMake(WIDTH - 200, 10) MyFontSize:9];
    _time.frame = CGRectMake(WIDTH - timeWidth - 10, _path.frame.origin.y + _path.frame.size.height + 15, timeWidth, 10);
    _line.frame = CGRectMake(40, _clubName.frame.origin.y + _clubName.frame.size.height + 15 , WIDTH - 40, 0.3);
}
#pragma mark-计算文本高度
//计算文本高
+ (CGFloat)heightWithText:(NSString *)text
{
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:13]};
    //最大范围
    CGSize size = CGSizeMake(WIDTH - 50, 1000);
    //计算 文本 需的rectangular
    CGRect r = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    //返回文本 的 高
    return r.size.height;
}
#pragma mark - 计算文本宽
//计算文本宽
+ (CGFloat)widthWithText:(NSString *)text MySize:(CGSize)size MyFontSize:(CGFloat)fontSize
{
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]};
    //    CGSize size = CGSizeMake(WIDTH - 100, 10);
    CGRect r = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return r.size.width;
}
#pragma mark -距离现在多久
//计算时间 转换成 距离现在 多久
+ (NSString *)compareCurrentTime:(NSNumber *)date
{
    // 返回以1970/01/01 GMT为基准，然后过了secs秒的时间
    NSDate *compareDate = [NSDate dateWithTimeIntervalSince1970:[date doubleValue]];
    // 以当前时间(Now)为基准时间，返回实例保存的时间与当前时间(Now)的时间间隔
    NSTimeInterval  timeInterval = [compareDate timeIntervalSinceNow];
    timeInterval = -timeInterval;
    long temp = 0;
    NSString *result;
    if (timeInterval < 60) {
        result = [NSString stringWithFormat:@"刚刚"];
    }
    else if((temp = timeInterval / 60) < 60){
        result = [NSString stringWithFormat:@"%ld分钟前", temp];
    }
    
    else if((temp = temp / 60) < 24){
        result = [NSString stringWithFormat:@"%ld小时前", temp];
    }
    
    else if((temp = temp / 24) < 30){
        result = [NSString stringWithFormat:@"%ld天前", temp];
    }
    
    else if((temp = temp / 30) < 12){
        result = [NSString stringWithFormat:@"%ld月前", temp];
    }
    else{
        temp = temp / 12;
        result = [NSString stringWithFormat:@"%ld年前", temp];
    }
    
    return  result;
}
#pragma mark- 点击放大
-(void)showZoomImageView:(UITapGestureRecognizer *)tap
{
    if (![(UIImageView *)tap.view image]) {
        return;
    }
    //scrollView作为背景
    UIScrollView *bgView = [[UIScrollView alloc] init];
    bgView.frame = [UIScreen mainScreen].bounds;
    bgView.backgroundColor = [UIColor blackColor];
    bgView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapBg = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBgView:)];
    [bgView addGestureRecognizer:tapBg];
    
    UIImageView *picView = (UIImageView *)tap.view;
    
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = picView.image;
    imageView.frame = [bgView convertRect:picView.frame fromView:self];
    [bgView addSubview:imageView];
    
    [[[UIApplication sharedApplication] keyWindow] addSubview:bgView];
    
    self.lastImageView = imageView;
    self.originalFrame = imageView.frame;
    self.scrollView = bgView;
    //最大放大比例
    self.scrollView.maximumZoomScale = 1.5;
    self.scrollView.delegate = self;
    [UIView animateWithDuration:0.5 animations:^{
        CGRect frame = imageView.frame;
        frame.size.width = bgView.frame.size.width;
        frame.size.height = frame.size.width * (imageView.image.size.height / imageView.image.size.width);
        frame.origin.x = 0;
        frame.origin.y = (bgView.frame.size.height - frame.size.height) * 0.5;
        imageView.frame = frame;
        if (frame.size.height > HEIGHT) {
            _scrollView.contentSize = CGSizeMake(frame.size.width, frame.size.height);
            frame.origin.x = 0;
            frame.origin.y = 0;
            imageView.frame = frame;
        }
        
    }];
    
}

-(void)tapBgView:(UITapGestureRecognizer *)tapBgRecognizer
{
    self.scrollView.contentOffset = CGPointZero;
    [UIView animateWithDuration:0.5 animations:^{
        self.lastImageView.frame = self.originalFrame;
        tapBgRecognizer.view.backgroundColor = [UIColor clearColor];
    } completion:^(BOOL finished) {
        [tapBgRecognizer.view removeFromSuperview];
        self.scrollView = nil;
        self.lastImageView = nil;
    }];
}



//返回可缩放的视图
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return _scrollView.subviews.firstObject;
}



- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
